import { fetchList } from ".";
import { drawerLoading, drawerVisible } from "./drawer";
import {
  drawerFormState,
  operateType,
  searchPayCapacityOptions,
  searchPayTypeOptions,
} from "./store";
export const handleSubmit = async () => {
  const loading = drawerLoading;
  const visible = drawerVisible;
  try {
    if (operateType.value === "view") {
      loading.value = true;
    }
    if (operateType.value === "add") {
    }
    if (operateType.value === "update") {
      loading.value = true;
      const res: any = await tripartiteApi.update({
        id: drawerFormState.id,
        name: drawerFormState.name,
        code: drawerFormState.code,
        secret_key: drawerFormState.secret_key,
        pay_merchant_no: drawerFormState.pay_merchant_no,
        is_support_pay: drawerFormState.is_support_pay,
        pay_gateway_url: drawerFormState.pay_gateway_url,
        support_pay_passageways:
          drawerFormState?.support_pay_passageways?.join(","),
        is_support_withdraw: drawerFormState.is_support_withdraw,
        support_withdraw_passageways:
          drawerFormState.support_withdraw_passageways,
        withdraw_gateway_url: drawerFormState.withdraw_gateway_url,
        sort: drawerFormState.sort,
        comment: drawerFormState.comment,

        is_disabled: drawerFormState.is_disabled,
      });
      if (res.code != 200) return message.error(res.message);
      message.success($t("base.updateSucess"));
      fetchList();
    }
  } catch (e: any) {
    message.error(e.message);
  } finally {
    loading.value = false;
    visible.value = false;
  }
};

export const fetchSearchMap = async () => {
  try {
    const res: any = await tripartiteApi.quertySearchDicts();
    if (res.code != 200) return message.error(res.message);
    searchPayCapacityOptions.value = [
      { label: $t("tripartite36"), value: Infinity },
      ...objectToOptions(res.data.abilityToPayList, "number"),
    ];
    searchPayTypeOptions.value = [
      { label: $t("tripartite36"), value: Infinity },
      ...(Object.values(res.data?.passagewayList)?.map((item: any) => ({
        label: item,
        value: item,
      })) ?? []),
    ];
  } catch (error: any) {
    message.error(error.message);
  }
};
